package 设计模式.抽象工厂模式;

import com.sun.org.apache.xpath.internal.operations.Or;

/**
 * @description: 抽象工厂模式（模拟数据库连接流程 包括：连接池，获取连接，发送命令）
 * 抽象工厂模式类似工厂方法模式 实际上：抽象工厂囊括了一组工厂方法（即是工厂方法模式的组合）
 * 特点：
 * 确保工厂中的方法是兼容的
 * 解耦
 * 符合单一职责原则:强调关注点
 * 符合开闭原则
 * @author: Kk
 * @create: 2020-12-08 10:21
 **/
public class AbstractFactory {
    public static void main(String[] args) {
        IDatabaseUtils databaseUtils=new OracleDatabaseUtils();
        IConnection connection = databaseUtils.getConnection();
        connection.connect();
        ICommand command = databaseUtils.getCommand();
        command.command();
    }
}

//抽象接口
interface IConnection{
    void connect();
}
interface ICommand{
    void command();
}
interface IDatabaseUtils{
    IConnection getConnection();
    ICommand getCommand();
}

//实现类
class MysqlConnection implements IConnection{
    @Override
    public void connect() {
        System.out.println("Mysql connected.");
    }
}

class OracleConnection implements IConnection{
    @Override
    public void connect() {
        System.out.println("Oracle connected.");
    }
}

class MysqlCommand implements ICommand{
    @Override
    public void command() {
        System.out.println("Mysql command.");
    }
}

class OracleCommand implements ICommand{
    @Override
    public void command() {
        System.out.println("Oracle command.");
    }
}
//实现mysql产品
class MysqlDatabaseUtils implements IDatabaseUtils{

    @Override
    public IConnection getConnection() {
        return new MysqlConnection();
    }

    @Override
    public ICommand getCommand() {
        return new MysqlCommand();
    }
}

//实现Oracle产品
class OracleDatabaseUtils implements IDatabaseUtils{

    @Override
    public IConnection getConnection() {
        return new OracleConnection();
    }

    @Override
    public ICommand getCommand() {
        return new OracleCommand();
    }
}

